package com.netease.xone.itemview;

import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import Utils.StackTraceUtil;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;

import com.netease.log.NTLog;
import com.netease.xone.R;


public class RenderInfoBase implements IRenderInfoBase{
    private static final String TAG = RenderInfoBase.class.getSimpleName();
    
    private View mHead;
    private ViewGroup mBody;
    private View mFoot;

    public RenderInfoBase(View v) {
        mHead = v.findViewById(R.id.head);
        mBody = (ViewGroup)v.findViewById(R.id.body);
        mFoot = v.findViewById(R.id.foot);
    }

    @Override
    public void renderView(Context context, AppInfo appInfo) {
        if (null == context || null == appInfo) {
            return;
        }
        
        renderHead(context, appInfo);
        renderBody(context, appInfo);
        renderFoot(context, appInfo);
    }
    
    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
        // 正文模式下
        mFoot.setVisibility(View.GONE);
        
        renderHead(context, appInfoDetail);
        renderBody(context, appInfoDetail);
    }
    
    public static void renderView(IRenderInfoBase iRenderInfoBase, Context context, AppInfo appInfo) {
        if (null == iRenderInfoBase) {
            return;
        }
        iRenderInfoBase.renderView(context, appInfo);
    }
    
    public static void renderView(IRenderInfoBase iRenderInfoBase, Context context, AppInfoDetail appInfoDetai) {
        if (null == iRenderInfoBase) {
            return;
        }
        iRenderInfoBase.renderView(context, appInfoDetai);
    }
    
    private IRenderInfoBase getHead() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        IRenderInfoBase iRenderInfoBase = null;
        if (null == mHead.getTag()) {
            iRenderInfoBase = new RenderInfoHead(mHead);
            mHead.setTag(iRenderInfoBase);
        } else if (null != mHead.getTag() && mHead.getTag() instanceof RenderInfoHead) {
            iRenderInfoBase = (IRenderInfoBase)mHead.getTag();
        }
        
        return iRenderInfoBase;
    }
    
    private void renderHead(Context context, AppInfo appInfo) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        IRenderInfoBase iRenderInfoBase = getHead();
        
        renderView(iRenderInfoBase, context, appInfo);
    }

    private void renderHead(Context context, AppInfoDetail appInfoDetail) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        IRenderInfoBase iRenderInfoBase = getHead();
        
        renderView(iRenderInfoBase, context, appInfoDetail);
    }

    private void renderBody(Context context, AppInfo appInfo, AppInfoDetail appInfoDetail) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        int type = 0;
        
        if (null != appInfo && null != appInfo.getInfoType()) {
            type = appInfo.getInfoType();
        } else if (null != appInfoDetail && null != appInfoDetail.getInfoType()) {
            type = appInfoDetail.getInfoType();
        } else {
            return;
        }
        
        IRenderInfoBase iRenderInfoBase = null;
        LayoutParams params = null;
        View v = null;
        switch (type) {
            case AppInfo.TYPE_DISCUSS:
                if (null != mBody.getTag() && mBody.getTag() instanceof RenderInfoDiscuss) {
                    iRenderInfoBase = (IRenderInfoBase)mBody.getTag();
                } else {
                    v = View.inflate(context, R.layout.item_view_info_discuss, null);
                    iRenderInfoBase = new RenderInfoDiscuss(v);
                }
                
                break;

            default:
                if (null != mBody.getTag() && mBody.getTag() instanceof RenderInfoOfficialBase) {
                    iRenderInfoBase = (IRenderInfoBase)mBody.getTag();
                } else {
                    v = View.inflate(context, R.layout.item_view_info_official_base, null);
                    iRenderInfoBase = new RenderInfoOfficialBase(v);
                }
                break;
        }

        if (null != appInfo) {
            renderView(iRenderInfoBase, context, appInfo);
        }
        
        if (null != appInfoDetail) {
            renderView(iRenderInfoBase, context, appInfoDetail);
        }
        
        if (null != v) {
            mBody.removeAllViews();
            params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            mBody.setTag(iRenderInfoBase);
            mBody.addView(v, params);
        }
    }
    
    private void renderBody(Context context, AppInfo appInfo) {
        renderBody(context, appInfo, null);
    }

    private void renderBody(Context context, AppInfoDetail appInfoDetail) {
        renderBody(context, null, appInfoDetail);
    }
    
    private void renderFoot(Context context, AppInfo appInfo) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        IRenderInfoBase iRenderInfoBase = null;
        
        if (null == mFoot.getTag()) {
            iRenderInfoBase = new RenderInfoFoot(mFoot);
            mFoot.setTag(iRenderInfoBase);
        } else if (null != mFoot.getTag() && mFoot.getTag() instanceof RenderInfoFoot) {
            iRenderInfoBase = (IRenderInfoBase)mFoot.getTag();
        }
        
        renderView(iRenderInfoBase, context, appInfo);
    }
}
